import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import {
  getServicesTempaltes,
  addServicesTempalte,
  getShopServices,
  addServicesTempalteItem,
  getVipCardList,
  queryContacts,
  addVipCard,
  updateVipCard,
  addVipCardHistory,
  delVipCard,
  getShopInfo,
  getVipCardHistoryList,
  addPackage,
  delOneServiceItem,
  delPackageTemplate,
  editServicesTemplate,
} from '@/services/http';

export default {
  namespace: 'vip',
  state: {
    templateList: [],
    newsList:[],
    total: 0,
    isLoading: true,
    ret: {},
    vipList:[],
    vipLisTotal:0,
    historyList:[],
  },

  effects: {

    *getServicesTempaltes(payload, { call, put }) {
      const response = yield call(getServicesTempaltes, payload.payload);
      yield put({
        type: '_getServicesTempaltes',
        data: response,
      })
    },

    *getShopServices(payload, { call, put }) {
      const {callback} =  payload.payload
        const response = yield call(getShopServices, payload.payload);
        callback(response.ret)
    },

    *addServicesTempalte(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(addServicesTempalte, payload.payload);
      callback(response);
    },

    *editServicesTemplate(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(editServicesTemplate, payload.payload);
      callback(response);
    },

    *addPackage(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(addPackage, payload.payload);
      callback(response);
    },

    *addServicesTempalteItem(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(addServicesTempalteItem, payload.payload);
      callback(response);
    },

    *getVipCardList(payload, { call, put }) {
      const {callback} =  payload.payload
      const response = yield call(getVipCardList, payload.payload);
      yield put({
        type: '_getVipCardList',
        data: response,
      })
    },

    *getVipCardHistoryList(payload, { call, put }) {
      const {callback} =  payload.payload
      const response = yield call(getVipCardHistoryList, payload.payload);
      yield put({
        type: '_getVipCardHistoryList',
        data: response,
      })
    },

    *queryContacts(payload, { call, put }) {
      const {callback} =  payload.payload
      const response = yield call(queryContacts, payload.payload);
      callback(response.ret)
    },

    *addVipCard(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(addVipCard, payload.payload);
      callback(response);
    },

    *updateVipCard(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(updateVipCard, payload.payload);
      callback(response);
    },

    *addVipCardHistory(payload, { call, put }) {
      const response = yield call(addVipCardHistory, payload.payload);
    },

    *delVipCard(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(delVipCard, payload.payload);
      callback(response);
    },

    *getShopInfo(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(getShopInfo, payload.payload);
      callback(response);
    },

    *delOneServiceItem(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(delOneServiceItem, payload.payload);
      callback(response)
    },

    *delPackageTemplate(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(delPackageTemplate, payload.payload);
      callback(response)
    },



  },

  reducers: {
    _getServicesTempaltes(state, action) {
      return {
        ...state,
        templateList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },
    _getVipCardList(state, action) {
      return {
        ...state,
        vipList: action.data.code == 1 ? action.data.ret : [],
        vipLisTotal: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },
    _getVipCardHistoryList(state, action) {
      return {
        ...state,
        historyList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },

  },
};
